package pathfinding.gui.listeners;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import pathfinding.gui.Map;
import pathfinding.gui.PathFindingApplet;

public class ZoomWheelListener implements MouseWheelListener {

	private static final int MAP_ZOOM = 50;

	private PathFindingApplet master;
	
	public ZoomWheelListener(PathFindingApplet master) {
		this.master = master;
	}
	
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		
		int zoomAmount = MAP_ZOOM;
		
		int notches = e.getWheelRotation();
		if (notches > 0) {
			zoomAmount *= -1;
		}
		else {
			zoomAmount = Math.abs(zoomAmount);
		}
		
		JTabbedPane tabPane = this.master.getTabbedPane();
		if (! (tabPane.getSelectedComponent() instanceof JScrollPane)) {
			return;
		}
		
		JScrollPane jscroll = (JScrollPane)tabPane.getSelectedComponent();
		if (! (jscroll.getViewport().getView() instanceof Map)) {
			return;
		}
		
		Map map = (Map) jscroll.getViewport().getView();
		
		Dimension size = map.getSize();
		int width = (int)size.getWidth();
		int height = (int)size.getHeight();
		
		// to prevent setting map size too small
		if (zoomAmount < 0 && width < jscroll.getVisibleRect().getWidth() && 
			height < jscroll.getVisibleRect().getHeight()) {
			return;
		}

		size.setSize(width + zoomAmount, height + zoomAmount);
		map.setPreferredSize(size);
		map.setSize(size);
		map.validate();	
		
	}



}
